/**
 * 增加一行信息
 */
function addRow(){
   var root = document.getElementById("tbody");
   var allRows = root.getElementsByTagName('tr');
   var cRow = allRows[1].cloneNode(true);
   cRow.style.display="";
   root.appendChild(cRow);
   //selectCurrencys(cRow);
}

/**
 * 删除一行信息
 */
function removeRow(r)
{
    var root = r.parentNode;
    var allRows = root.getElementsByTagName('tr');
    if(allRows.length > 2){
    	root.removeChild(r);
    }
    zjs(); 
}

/**
 * 打开借款单选择器
 */
function openSelectborrows(type){
	var reimbTotal = document.getElementById("reimbTotal").value;
	var balance = document.getElementById("balance").value;
    if (Number(balance) == 0 ) {
       alert("余款金额为0,不允许选择借款单进行销账业务！");	
       return;
    }
    
	var totalBorrowIds = document.getElementById("totalBorrowIds").value;
	var partBorrowIds = document.getElementById("partBorrowIds").value;
	
	if (type == 'total'){
	  var url = URL_PREFIX + '/borrow/selectBorrowIndex.do?borrowIds=' + partBorrowIds + '&type=checkbox';
	} else {
	  var url = URL_PREFIX + '/borrow/selectBorrowIndex.do?borrowIds=' + totalBorrowIds + '&type=radio';
	}
	var styles = "dialogWidth:585px; dialogHeight:430px; status:no; scroll:no; help:no";
	var selectedBorrows = window.showModalDialog(url, null, styles);
	
    var totalAmount;
    var totalRepayAmount;
    var totalNoRepayAmount;
    var partAmount;
    var partRepayAmount;
    var partNoRepayAmount;
	if (!selectedBorrows){
		//如果没有选择用户不做任何操作
	}else{
		if (type == 'total'){
		  totalAmount = 0;
		  totalRepayAmount = 0;
		  totalNoRepayAmount = 0;	
		  $("#totalBorrowNos").val(selectedBorrows.nos);
		  $("#totalBorrowIds").val(selectedBorrows.ids); 
		  $("#totalBorrowAmounts").val(selectedBorrows.borrowAmounts); 
		  $("#totalRepayAmounts").val(selectedBorrows.rePayAmounts); 
		  var borrowAmountsStr = $("#totalBorrowAmounts").val();
	      var borrowAmounts = borrowAmountsStr.split(",");
	      for(var i = 0; i < borrowAmounts.length - 1; i++){
	    	  totalAmount += Number(borrowAmounts[i]);
	      }
		  
		  var rePayAmountsStr = $("#totalRepayAmounts").val();
		  var rePayAmounts = rePayAmountsStr.split(",");
	      for(var i = 0; i < rePayAmounts.length - 1; i++){
	    	  totalRepayAmount += Number(rePayAmounts[i]);
		  }
	      totalNoRepayAmount = Number(totalAmount) - Number(totalRepayAmount);
	      $("#totalNoRepayAmounts").val(totalNoRepayAmount.toFixed(2));
	      
	      if (Number($("#totalNoRepayAmounts").val()) == 0) {
			  $("#totalBorrowNos").val("");
			  $("#totalBorrowIds").val(""); 
			  $("#totalBorrowAmounts").val(""); 
			  $("#totalRepayAmounts").val(""); 
			  $("#totalNoRepayAmounts").val("");
			  
			  $("#partBorrowNos").val("");
			  $("#partBorrowIds").val(""); 
			  $("#partBorrowAmounts").val(""); 
			  $("#partRepayAmounts").val(""); 
			  $("#partNoRepayAmounts").val("");
			  $("#borrowAmount").val("");
			  zjs(); 
			  return false; 
	      }
	      
			 if (Number($("#partNoRepayAmounts").val()) > 0) {
				 if (Number($("#totalNoRepayAmounts").val()) > 0 && reimbTotal - Number($("#partNoRepayAmounts").val()) < Number($("#totalNoRepayAmounts").val())) {
					  alert("报销金额小于销账金额,请您操作部分销账业务！");
					  $("#totalBorrowNos").val("");
					  $("#totalBorrowIds").val(""); 
					  $("#totalBorrowAmounts").val(""); 
					  $("#totalRepayAmounts").val(""); 
					  $("#totalNoRepayAmounts").val("");
					  
					  $("#partBorrowNos").val("");
					  $("#partBorrowIds").val(""); 
					  $("#partBorrowAmounts").val(""); 
					  $("#partRepayAmounts").val(""); 
					  $("#partNoRepayAmounts").val("");
					  $("#borrowAmount").val("");
					  zjs(); 
					  return false;
				 }
			 } else {
				 if (Number($("#totalNoRepayAmounts").val()) > 0 && reimbTotal < Number($("#totalNoRepayAmounts").val())) {
					  alert("报销金额小于销账金额,请您操作部分销账业务！");
					  $("#totalBorrowNos").val("");
					  $("#totalBorrowIds").val(""); 
					  $("#totalBorrowAmounts").val(""); 
					  $("#totalRepayAmounts").val(""); 
					  $("#totalNoRepayAmounts").val("");
					  
					  $("#partBorrowNos").val("");
					  $("#partBorrowIds").val(""); 
					  $("#partBorrowAmounts").val(""); 
					  $("#partRepayAmounts").val(""); 
					  $("#partNoRepayAmounts").val("");
					  $("#borrowAmount").val("");
					  zjs(); 
					  return false;
				 }
			 }
		} else {
		  partAmount = 0;
		  partRepayAmount = 0;
		  partNoRepayAmount = 0;			
		  $("#partBorrowNos").val(selectedBorrows.nos);
		  $("#partBorrowIds").val(selectedBorrows.ids);
		  $("#partBorrowAmounts").val(selectedBorrows.borrowAmounts); 
		  $("#partRepayAmounts").val(selectedBorrows.rePayAmounts); 
		  var borrowAmountsStr = $("#partBorrowAmounts").val();
	      var borrowAmounts = borrowAmountsStr.split(",");
	      for(var i = 0; i < borrowAmounts.length - 1; i++){
	    	  partAmount += Number(borrowAmounts[i]);
	      }
		  
		  var rePayAmountsStr = $("#partRepayAmounts").val();
		  var rePayAmounts = rePayAmountsStr.split(",");
	      for(var i = 0; i < rePayAmounts.length - 1; i++){
	    	  partRepayAmount += Number(rePayAmounts[i]);
		  }
	      partNoRepayAmount = Number(partAmount) - Number(partRepayAmount);
	      $("#partNoRepayAmounts").val(partNoRepayAmount.toFixed(2));
	      
	      if (Number($("#partNoRepayAmounts").val()) == 0) {
			  $("#totalBorrowNos").val("");
			  $("#totalBorrowIds").val(""); 
			  $("#totalBorrowAmounts").val(""); 
			  $("#totalRepayAmounts").val(""); 
			  $("#totalNoRepayAmounts").val("");
			  
			  $("#partBorrowNos").val("");
			  $("#partBorrowIds").val(""); 
			  $("#partBorrowAmounts").val(""); 
			  $("#partRepayAmounts").val(""); 
			  $("#partNoRepayAmounts").val("");
			  $("#borrowAmount").val("");
			  zjs(); 
			  return false; 
	      }
	      
			 if (Number($("#totalNoRepayAmounts").val()) > 0) {
				 if (Number($("#partNoRepayAmounts").val()) > 0 && reimbTotal - Number($("#totalNoRepayAmounts").val()) >= Number($("#partNoRepayAmounts").val())) {
					  alert("报销金额已大于或等于部分销账的未销账金额,请您操作销账业务！");
					  $("#partBorrowNos").val("");
					  $("#partBorrowIds").val(""); 
					  $("#partBorrowAmounts").val(""); 
					  $("#partRepayAmounts").val(""); 
					  $("#partNoRepayAmounts").val("");
					  
					  $("#totalBorrowNos").val("");
					  $("#totalBorrowIds").val(""); 
					  $("#totalBorrowAmounts").val(""); 
					  $("#totalRepayAmounts").val(""); 
					  $("#totalNoRepayAmounts").val("");
					  $("#borrowAmount").val("");
					  zjs(); 
					  return false; 
				 }
			 } else {
				 if (Number($("#partNoRepayAmounts").val()) > 0 && reimbTotal >= Number($("#partNoRepayAmounts").val())) {
					  alert("报销金额已大于或等于部分销账的未销账金额,请您操作销账业务！");
					  $("#partBorrowNos").val("");
					  $("#partBorrowIds").val(""); 
					  $("#partBorrowAmounts").val(""); 
					  $("#partRepayAmounts").val(""); 
					  $("#partNoRepayAmounts").val("");
					  
					  $("#totalBorrowNos").val("");
					  $("#totalBorrowIds").val(""); 
					  $("#totalBorrowAmounts").val(""); 
					  $("#totalRepayAmounts").val(""); 
					  $("#totalNoRepayAmounts").val("");
					  $("#borrowAmount").val("");
					  zjs(); 
					  return false; 
				 }
			 }	
		}
		 $("#offsetAmount").val(toFix(reimbTotal - Number($("#totalNoRepayAmounts").val())));		 
         var borrowTotal = Number($("#totalNoRepayAmounts").val()) + Number($("#partNoRepayAmounts").val());
         
         if (borrowTotal > reimbTotal) {
        	 $("#borrowAmount").val(reimbTotal);	 
         } else {
        	 $("#borrowAmount").val(borrowTotal);
         }
         
         if (reimbTotal > borrowTotal) {
        	$("#balance").val(toFix(reimbTotal - borrowTotal));
         } else {
        	$("#balance").val(0); 
         }
	}
}

function toFix(idvalue){
   return parseFloat(idvalue).toFixed(2);//2表示保留2位小數 
}

/**
 * 验证借款金额输入的必须是数字
 */
function yzsz(amount, r){
	if(isNaN(amount.value)||amount.value-0<0){
		alert("请输入数字并且大于0！");
		amount.value="";
		return;
	}
	zjs(); 
}

/**
 * 验证借款外币金额输入的必须是数字
 */
function yzwbsz(amount, r){
	var tab = document.getElementsByName("cName") ;
	if(isNaN(amount.value) || amount.value - 0 < 0){
		alert("请输入数字并且大于0！");
		amount.value="";
		return;
	}
	
	if (tab[r.rowIndex-1].value != null && tab[r.rowIndex-1].value != "") {
		var cName = tab[r.rowIndex-1].value;
		currencyRate(r, cName.split(",")[0]);
	} else {
		alert("请您先选择外币币种！");
		amount.value="";
		selectCurrencys(r);
		return;
	}
	zjs(); 
}

/**
 * 计算总金额
 */
function zjs(){
 var tab1 = document.getElementsByName("amount") ;
 var amount = 0;

 for(var i = 1; i < tab1.length; i++){
   amount += Number(tab1[i].value);
 }

 var reimbTotal = Number(amount);
 document.getElementById("reimbTotal").value = reimbTotal.toFixed(2);

 var borrowAmount = document.getElementById("borrowAmount").value; 
 if (reimbTotal > Number(borrowAmount)) {
	$("#balance").val((reimbTotal - Number(borrowAmount)).toFixed(2));
 } else {
	$("#balance").val("0.00"); 
 }
}

/**
 * 点击项目类别后加载所有的类别信息
 */
 function selectCurrencys(r){
   var tab = document.getElementsByName("cName") ;
   showCurrencys(tab[r.rowIndex-1]);	
 }

/**
 * 加载所有的类别信息
 */
function showCurrencys(rr){
	var r = rr.parentNode.parentNode;
		Ext.Ajax.request({
			url : '/reimb/getCurrencys.do',
			params : {
			},
			method : 'POST',
			success : function(response) {
			    var jsonResult = Ext.util.JSON.decode(response.responseText);
			        var result = jsonResult.result ;
			        var currencys = result.split(":");
			        var html = "<select name=\'cId\' onchange=\'currencyChange(this)\' style=\'width: 90px\'>";
			        var currency;
			        for(var i = 0; i < currencys.length - 1; i++){
			        	currency = currencys[i].split(",");
			           	html = html + "<option value=\'" + currencys[i] + "\' ";
			           	if(rr.value == currency[1]){
			           		html = html + " selected=\'selected\' ";
			           	}
			
			           	html = html + " >" + currency[1] +"</option>";
			           }
			        rr.parentNode.innerHTML=html+"</select><input name=\'cName\' type=\'hidden\'/>";
			    }
		});
}

/**
 * 加载币种汇率信息
 */
function currencyRate(r, currencyId){
	var cId = document.getElementsByName("cId");
	var currencyId = cId[r.rowIndex-1].value.split(",")[0];
		Ext.Ajax.request({
			url : '/reimb/getCurrencyRate.do',
			params : {
				currencyId : currencyId
			},
			method : 'POST',
			success : function(response) {
			    var jsonResult = Ext.util.JSON.decode(response.responseText);
			        var result = jsonResult.result ;
			        if (result != null && result != 'failure') {
			          var tab = document.getElementsByName("amount") ;	
			       	  var tab1 = document.getElementsByName("exchangeAmount");
			       	  //根据外币金额和币种的汇率计算兑换的本币金额
                     var amount = Number(tab1[r.rowIndex-1].value) * Number(result); 
                     tab[r.rowIndex-1].value = amount.toFixed(2);
                     zjs();                     
			        } else if (result == 'failure') {
			          alert("无法获取汇率，请您检查该币种汇率表中的数据！");
			        } else if (result == null) {
			        	tab1[r.rowIndex-1].value = "";
			        }
			    }
		});
}

/**
 * 根据选择的币种变更币种信息
 */
function currencyChange(currency){
 var r = currency.parentNode.parentNode;
 var cName = document.getElementsByName("cName") ;
 var amount = document.getElementsByName("amount") ;
 var exchangeAmount = document.getElementsByName("exchangeAmount") ;
 if (currency.value.split(",")[0] == "") {
	 amount[r.rowIndex-1].value = "";
	 exchangeAmount[r.rowIndex-1].value = "";
	 zjs(); 
	 return false;
 }
 cName[r.rowIndex-1].value = currency.value;
 currencyRate(r, cName[r.rowIndex-1].value.split(",")[0]);
}